package com.power.sifa.info.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.info.model.Law_pubunit;
import com.power.sifa.info.model.pageInfo.Law_pubunitPageInfo;
import com.power.sifa.info.api.ILaw_pubunitFeign;
import com.power.sifa.info.service.Law_pubunitService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   法律发布机关控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Law_pubunitController",tags={"法律发布机关"})
public class Law_pubunitController implements ILaw_pubunitFeign{

    @Resource
    private Law_pubunitService law_pubunitService;

    
    @Override
    @ApiOperation("查询法律发布机关分页列表")
    @SaCheckPermission("law_pubunit_list")
    public ResponseDTO list(Law_pubunitPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Law_pubunit> page = this.law_pubunitService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Law_pubunit law_pubunit : page) {
                law_pubunit.geneText();
                list.add(law_pubunit);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看全部的法律发布机关，不带条件查询")
    public ResponseDTO allList() {
        return ResponseDTO.succData(this.law_pubunitService.findAll());
    }


    @Override
    @ApiOperation("查看法律发布机关")
    @SaCheckPermission("law_pubunit_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Law_pubunit law_pubunit  = law_pubunitService.getById(id);
        if (law_pubunit != null) {
            law_pubunit.geneText();
        }
        return ResponseDTO.succData(law_pubunit);
    }

    @Override
    @ApiOperation("删除法律发布机关")
    @SaCheckPermission("law_pubunit_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        law_pubunitService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改法律发布机关")
    @SaCheckPermission("law_pubunit_save")
    public ResponseDTO save(Law_pubunit law_pubunit) {
        Assert.notNull(law_pubunit, "law_pubunit不能为空");
        law_pubunitService.saveOrUpdate(law_pubunit);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除法律发布机关")
    @SaCheckPermission("law_pubunit_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        law_pubunitService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("law_pubunit_list")
    public void output(HttpServletResponse response, Law_pubunitPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "法律发布机关";
        String[] heads = {"机关编码","机关名称","区域编码","类型","删除标记"};
        String[] fields = {"id","name","adid","type","deleted"};
        Page<Law_pubunit> page = this.law_pubunitService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "法律发布机关";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"机关编码","机关名称","区域编码","类型","删除标记"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","adid","type","deleted"};
        List<Law_pubunit> list = (List<Law_pubunit>)ExcelUtil.Import(fields, file, Law_pubunit.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.law_pubunitService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
